/*************************************************************************
	> File Name: status.cpp
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年10月18日 星期四 16时00分48秒
 ************************************************************************/
#include "status.h"
#include <stdlib.h>
#include <utils/dfile.h>

status::status(int id):
    m_id(id),
    m_lock(true){

#if 0
//    create_table();
    if (!exist()) {
        char cond[MAX_COND_LEN] = {0};

        sprintf(cond,"%d,0,0,''",id);
        m_sqlite.Insert("id,status,onoff,acqdata",cond);
    }
#endif
}

bool status::istatus()
{
#if 0
    std::string stat = operator []("status");
    if (stat.empty())
        return false;
    else {
        return (bool)(atoi(stat.c_str()) != 0);
    }
#endif
    int len = 30+strlen(getenv(CONFIG_DIR_ENV));
    char *filename = (char*)malloc(len);
    memset(filename,0,len);
    sprintf(filename,"%s/data/STATUS%04d.stat",getenv(CONFIG_DIR_ENV),m_id);

    AutoLock autolock(m_lock);
    if(!utils::file_exist(filename))
    {
        free(filename);
        return false;
    }
    bool st = false;
    utils::load_file(filename,0,(char*)&st,1,sizeof(st));
    free(filename);
    return st;
}

bool status::ionoff()
{
#if 0
    std::string stat = operator []("onoff");
    if (stat.empty())
        return false;
    else {
        return (bool)(atoi(stat.c_str()) != 0);
    }
#endif
    int len = 30+strlen(getenv(CONFIG_DIR_ENV));
    char *filename = (char*)malloc(len);
    memset(filename,0,len);
    sprintf(filename,"%s/data/STATUS%04d.stat",getenv(CONFIG_DIR_ENV),m_id);

    AutoLock autolock(m_lock);
    if(!utils::file_exist(filename))
    {
        free(filename);
        return false;
    }
    bool st = false;
    utils::load_file(filename,sizeof(st),(char*)&st,1,sizeof(st));
    free(filename);
    return st;
}

string status::iacqdata()
{
#if 0
    return operator []("acqdata");
#endif
    int len = 30+strlen(getenv(CONFIG_DIR_ENV));
    char *filename = (char*)malloc(len);
    memset(filename,0,len);
    sprintf(filename,"%s/data/STATUS%04d.stat",getenv(CONFIG_DIR_ENV),m_id);

    AutoLock autolock(m_lock);
    if(!utils::file_exist(filename))
    {
        free(filename);
        return "";
    }
    int length = 0;
    utils::load_file(filename,2*sizeof(bool),(char*)&length,1,sizeof(int));
    if (length <= 0) {
        free(filename);
        return "";
    }
    char *buffer = (char*)malloc(length+1);
    memset(buffer,0,length+1);
    utils::load_file(filename,2*sizeof(bool)+sizeof(int),buffer,1,length);
    string str = buffer;
    free(buffer);
    return str;
}

bool status::update_onoff(bool ison)
{
#if 0
    char stron[30] = {0};
    sprintf(stron,"%d",(int)ison);
    return update("onoff",stron);
#endif
    int len = 30+strlen(getenv(CONFIG_DIR_ENV));
    char *filename = (char*)malloc(len);
    memset(filename,0,len);
    sprintf(filename,"%s/data/STATUS%04d.stat",getenv(CONFIG_DIR_ENV),m_id);

    AutoLock autolock(m_lock);

    utils::save_file(filename,sizeof(bool),(char*)&ison,1,sizeof(bool));
    free(filename);
    return true;
}

bool status::update_status(bool sta)
{
#if 0
    char stron[30] = {0};
    sprintf(stron,"%d",(int)sta);
    return update("status",stron);
#endif
    int len = 30+strlen(getenv(CONFIG_DIR_ENV));
    char *filename = (char*)malloc(len);
    memset(filename,0,len);
    sprintf(filename,"%s/data/STATUS%04d.stat",getenv(CONFIG_DIR_ENV),m_id);

    AutoLock autolock(m_lock);

    utils::save_file(filename,0,(char*)&sta,1,sizeof(sta));
    free(filename);
    return true;
}

bool status::set_acqdata(const string &data)
{
#if 0
    std::string strdata;
    strdata += "'"+data+"'";
    return update("acqdata",strdata);
#endif
    int len = 30+strlen(getenv(CONFIG_DIR_ENV));
    char *filename = (char*)malloc(len);
    memset(filename,0,len);
    sprintf(filename,"%s/data/STATUS%04d.stat",getenv(CONFIG_DIR_ENV),m_id);

    AutoLock autolock(m_lock);

    int length = data.length();
    utils::save_file(filename,2*sizeof(bool),(char*)&length,1,sizeof(int));
    if (length <= 0) {
        free(filename);
        return false;
    }
    utils::save_file(filename,2*sizeof(bool)+sizeof(int),data.c_str(),1,length);
    free(filename);
    return true;
}

bool status::create_table()
{
    return false;
    //return m_sqlite.Create("id INT PRIMARY KEY NOT NULL,status INT,onoff INT,acqdata TEXT");
}
